package com.calvin.study.multithread.threadutils;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import com.calvin.study.utils.Print;

public class CyclicBarrierDemo {

	public static void main(String[] args) {
		//gatherLongzhu();
		lol();
	}
	
	private static void gatherLongzhu() {
		CyclicBarrier cb = new CyclicBarrier(7, () -> {
			System.out.println("龙珠集齐，召唤神龙");
		});

		for (int i = 1, len = 7; i <= len; i++) {
			final int temp = i;
			new Thread(() -> {
				System.out.println(Thread.currentThread().getName() + "\t 收集到第" + temp + "颗龙珠");
				try {
					cb.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (BrokenBarrierException e) {
					e.printStackTrace();
				}
			}, String.valueOf(i)).start();
		}
	}

	private static void lol() {
		CyclicBarrier cb = new CyclicBarrier(5, () -> {
			Print.tco("开团，爆操对面紫水晶");
		});

		String heros[] = { "德玛西亚", "皇子", "发条", "薇恩", "日女" };

		for (int i = 0, len = heros.length; i < len; i++) {
			int idx = i;
			new Thread(() -> {
				try {
					Print.tco(heros[idx]+"到达战场，准备开团");
					cb.await();
				} catch (Exception e) {
				}
			}, "thread-"+i).start();
		}
	} 

}
